package com.pro.xwt.common.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.pro.xwt.common.model.BMessage;
import com.pro.xwt.common.model.BTask;
import com.pro.xwt.common.service.BMessageService;
import com.pro.xwt.common.service.BTaskService;
import com.pro.xwt.common.utils.Common;

@Component("taskJob")
public class TaskJob {
	@Autowired
	BTaskService bTaskService;
	@Autowired
	BMessageService bMessageService;

	/**
	 * 每周循环任务定时器
	 */
	@Scheduled(cron = "0 0 0 ? * MON ")
	public void weekTask() {
		Map<String, Object> map = new HashMap<>();
		map.put("cycleTime", "WEEK");
		map.put("deleteState", "0");
		List<BTask> bTasks = bTaskService.queryListAll(map);
		if (bTasks.size() > 0) {
			insertTask(bTasks);
		}
	}

	/**
	 * 每月循环任务定时器
	 */
	@Scheduled(cron = "0 0 0 1 * ?")
	public void monthTask() {
		Map<String, Object> map = new HashMap<>();
		map.put("cycleTime", "MONTH");
		map.put("deleteState", "0");
		List<BTask> bTasks = bTaskService.queryListAll(map);
		if (bTasks.size() > 0) {
			insertTask(bTasks);
		}

	}

	/**
	 * 每天循环任务定时器
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	public void dayTask() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
			Map<String, Object> map = new HashMap<>();
			map.put("cycleTime", "DAY");
			map.put("deleteState", "0");
			List<BTask> bTasks = bTaskService.queryListAll(map);
			if (bTasks.size() > 0) {
				insertTask(bTasks);
			}
		}

	}

	@Scheduled(cron = "0 0 1 * * ?") // 每天早上1点触发
	public void task() {
		Date date = new Date();
		Map<String, Object> map = new HashMap<>();
		map.put("comState", "0");
		map.put("deleteState", "0");
		List<BTask> btasks = bTaskService.queryListAll(map);
		for (BTask bTask : btasks) {
			if ((int) ((bTask.getLastEndTime().getTime() - date.getTime()) / (1000 * 60 * 60 * 24)) < 1) {
				BMessage bm = new BMessage();
				String content = "你有一个新的任务即将逾期";
				bm.setId(Common.generateUUID());
				bm.setContent(content);
				bm.setType("DEADLINE");
				bm.setReadState("0");
				bm.setTaskId(bTask.getId());
				bm.setAccpEmpId(bTask.getExecEmpId());
				bm.setExpireTime(bTask.getLastEndTime());
				bm.setCreateTime(date);
				bm.setDeleteState("0");
				bm.setCreateEmpId(bTask.getCreateEmpId());
				bMessageService.insert(bm);
			}
		}
	}

	public void insertTask(List<BTask> bTasks) {
		Date date = new Date();
		for (BTask bTask : bTasks) {
			BTask bt = new BTask();
			if (bTask.getLastEndTime().getTime() >= date.getTime()) {
				// 任务截至时间
				if (bTask.getCycleTaskEnd() != null) {
					Date lastEndTime = new Date(date.getTime() + bTask.getCycleTaskEnd());
					bt.setLastEndTime(lastEndTime);
					bt.setCreateTime(date);
				}
				if (bTask.getName() != null) {
					bt.setName(bTask.getName());
				}
				bt.setId(Common.generateUUID());
				if (bTask.getProjId() != null) {
					bt.setProjId(bTask.getProjId());
				}
				if (bTask.getProjFlowId() != null) {
					bt.setProjFlowId(bTask.getProjFlowId());
				}
				if (bTask.getId() != null) {
					bt.setParentTaskId(bTask.getId());
				}
				if (bTask.getType() != null) {
					bt.setType(bTask.getType());
				}
				if (bTask.getDescription() != null) {
					bt.setDescription(bTask.getDescription());
				}
				if (bTask.getExecEmpId() != null) {
					bt.setExecEmpId(bTask.getExecEmpId());
				}
				if (bt.getExecEmpName() != null) {

					bt.setExecEmpName(bt.getExecEmpName());
				}
				if (bTask.getCreateEmpId() != null) {

					bt.setCreateEmpId(bTask.getCreateEmpId());
				}
				bt.setCycleTime("NONE");
				bt.setComState("0");
				bt.setDeleteState("0");
				bTaskService.insert(bt);
			}
		}
	}
}
